module.exports = (sequelize, DataTypes) => {
    const Handover = sequelize.define(
        'Handover',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true
            },
            date: {
                type: DataTypes.DATEONLY,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '交接日期不能为空'
                    },
                    isDate: {
                        args: true,
                        msg: '交接日期必须是一个日期格式'
                    }
                },
                comment: '交接日期'
            },
            shift: {
                type: DataTypes.TINYINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '交接班次不能为空'
                    }
                },
                comment: '交接班次: 1.早班 2.晚班'
            },
            nr_begin: {
                type: DataTypes.TIME,
                allowNull: true,
                comment: '日结开始时间'
            },
            nr_end: {
                type: DataTypes.TIME,
                allowNull: true,
                comment: '日结结束时间'
            },
            user_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '交接人员不能为空'
                    }
                },
                references: {
                    model: 'User',
                    key: 'id'
                },
                comment: '交接人员'
            },
            temperature: {
                type: DataTypes.FLOAT(5, 2),
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '温度不能为空'
                    },
                    min: {
                        args: -50,
                        msg: '温度不能低于-50度'
                    },
                    max: {
                        args: 100,
                        msg: '温度不能高于100度'
                    }
                },
                comment: '温度'
            },
            humidity: {
                type: DataTypes.TINYINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '湿度不能为空'
                    },
                    min: {
                        args: 1,
                        msg: '湿度不能低于1'
                    },
                    max: {
                        args: 100,
                        msg: '湿度不能高于100'
                    }
                },
                comment: '湿度'
            }
        },
        {
            tableName: 'handover'
        }
    )

    Handover.associate = function(models) {
        models.Handover.hasMany(models.Content, { foreignKey: 'handover_id', sourceKey: 'id' })
        models.Handover.belongsTo(models.User, { foreignKey: 'user_id', targetKey: 'id' })
    }

    return Handover
}
